package com.yang.leather.admin.action.prod;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yang.leather.admin.action.BaseAction;
import com.yang.leather.admin.pojo.Language;
import com.yang.leather.admin.pojo.ProdType;
import com.yang.leather.admin.service.IProdTypeService;
import com.yang.leather.common.util.ComUtils;

@Scope("prototype")
@Controller("admin.prodType")
public class ProdTypeAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private IProdTypeService prodTypeService;
	
	private List<ProdType> lstProdType;
	
	private ProdType prodType;
	
	public String list(){
		lstProdType = this.prodTypeService.getAllProdTypes();
		this.getRequest().setAttribute("lstProdType", lstProdType);

		return SUCCESS;
	}
	
	public String toAdd(){
		this.getRequest().setAttribute("languages", Language.getLanguage());

		return SUCCESS;
	}
	
	public String toEdit(){
		String sid = this.getRequest().getParameter("id");
		
		prodType = this.prodTypeService.findById(Long.valueOf(sid));
		
		this.getRequest().setAttribute("languages", Language.getLanguage());
		return SUCCESS;
	}
	
	public String save(){
		prodType.setCreateDate(ComUtils.now());
		prodType.setSort(1);
		this.prodTypeService.saveProdType(prodType);
		
		return SUCCESS;
	}
	
	public String edit(){
		this.prodTypeService.updateProdType(prodType);
		this.getRequest().setAttribute("languages", Language.getLanguage());
		
		return SUCCESS;
	}
	
	public String del(){
		String sid = this.getRequest().getParameter("id");
		
		this.prodTypeService.deleteProdType(Long.valueOf(sid));

		renderText("1");
		return null;
	}
	
	public List<ProdType> batchDel() {
		String ids = this.getRequest().getParameter("ids");
		List<Long> list = new ArrayList<Long>();
		for(String sId : ids.split(",")){
			list.add(Long.valueOf(sId));
		}
		this.prodTypeService.deleteProdTypes(list);
		
		renderText("1");
		return null;
	}

	public void setLstProdType(List<ProdType> lstProdType) {
		this.lstProdType = lstProdType;
	}

	public ProdType getProdType() {
		return prodType;
	}

	public void setProdType(ProdType prodType) {
		this.prodType = prodType;
	}

	public IProdTypeService getProdTypeService() {
		return prodTypeService;
	}

	@Autowired
	public void setProdTypeService(@Qualifier("prodTypeService")
			IProdTypeService prodTypeService) {
		this.prodTypeService = prodTypeService;
	}
	
}
